package forms;

import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import models.User;

public class UserForm {
	public static TreeMap checkValid(HttpServletRequest request, User user) {
		TreeMap form = new TreeMap();
		TreeMap checkValid = new TreeMap();
		
		String gmail = (String) request.getParameter("gmail");
		String fullname = (String) request.getParameter("fullname");
		String role = (String) request.getParameter("role");
		String status = (String) request.getParameter("status");
		String limitBlog = (String) request.getParameter("limitBlog");
		
		if (gmail == null || gmail.equals("")) {
			checkValid.put("gmail", "Gmail is not empty!");
			user.setGmail(gmail);
		} else {
			user.setGmail(gmail);
		}
		
		if (fullname == null || fullname.equals("")) {
			checkValid.put("fullname", "Fullname is not empty!");
			user.setName(fullname);
		} else {
			user.setName(fullname);
		}
		
		if (role != null) {
			try {
				user.setRole(Integer.parseInt(role));
			} catch (Exception e) {
				checkValid.put("role", "Role not valid!");
			}
		}
		
		if (status != null) {
			try {
				user.setStatus(Integer.parseInt(status));
			} catch (Exception e) {
				checkValid.put("status", "Status not valid!");
			}
		}
		
		if (limitBlog != null) {
			try {
				user.setLimitBlog(Integer.parseInt(limitBlog));
			} catch (Exception e) {
				checkValid.put("limitBlog", "Limit blog not valid!");
			}
		}
		
		
		form.put("checkValid", checkValid);
		form.put("user", user);
		return form;
	}
}
